Send Message

The MessageUI namespace provides functions to detect messaging capabilities and present a system message compose view from within a script. You can send SMS or MMS messages with optional subject and attachments, depending on device capabilities.

Availability Properties

MessageUI.isAvailable: boolean

Returns true if the device is capable of sending plain text messages.

1if (!MessageUI.isAvailable) {
2  console.log("This device cannot send messages.")
3}

MessageUI.canSendSubject: boolean

Returns true if the device supports adding a subject to messages.

MessageUI.canSendAttachments: boolean

Returns true if the device supports including attachments in messages.


MessageUI.present(options): Promise<"cancelled" | "sent" | "failed">

Displays the system’s message composer with the specified content and resolves with the result of the user’s action.

Parameters

Name Type Required Description
recipients string[] Yes An array of recipient phone numbers.
body string Yes The text content of the message body.
subject string No Optional subject line. Ignored if canSendSubject is false.
attachments Attachment[] No Optional list of attachments. Ignored if canSendAttachments is false.

Attachment Object

Each item in the attachments array must include:

Property Type Required Description
data Data Yes The binary data to be attached to the message.
type UTType Yes A Uniform Type Identifier string, such as "public.image" or "public.text".
fileName string Yes The name that will appear for the attachment in the message.

Return Value

Returns a Promise that resolves to one of the following values:

  • "sent": The message was successfully sent by the user.
  • "cancelled": The user canceled the message.
  • "failed": The message failed to send due to an error (e.g., connectivity or system failure).

Example: Basic Text Message

1if (MessageUI.isAvailable) {
2  const result = await MessageUI.present({
3    recipients: ["1234567890"],
4    body: "Hello from Scripting!"
5  })
6
7  console.log("Message result:", result) // sent, cancelled, or failed
8}

Example: Message with Subject and Attachment

1const fileData = Data.fromString("This is the document content.")
2
3if (MessageUI.isAvailable && MessageUI.canSendAttachments) {
4  const result = await MessageUI.present({
5    recipients: ["1234567890"],
6    body: "Here is the document.",
7    subject: "Requested File",
8    attachments: [
9      {
10        data: fileData,
11        type: "public.text",
12        fileName: "document.txt"
13      }
14    ]
15  })
16
17  if (result === "sent") {
18    console.log("Message successfully sent.")
19  } else {
20    console.log("Message was not sent:", result)
21  }
22}

Notes

  • The subject and attachments options are automatically ignored if not supported on the device.
  • This API only presents the UI; sending is user-controlled.
  • Can only be used in interactive scripts (not background-only scripts).